package com.geologyoa.servlet;
import java.io.File;  
import java.io.IOException;  
import java.io.PrintWriter;  
import java.util.Iterator;  
import java.util.List;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import org.apache.commons.fileupload.FileItem;  
import org.apache.commons.fileupload.FileUploadException;  
import org.apache.commons.fileupload.disk.DiskFileItemFactory;  
import org.apache.commons.fileupload.servlet.ServletFileUpload;  
@SuppressWarnings("serial")  
public class FileUpload extends HttpServlet   
{  
	private File uploadPath;      
    private int maxPostSize = 100*1024*1024;  
    public void init() throws ServletException {
		uploadPath = new File(getServletContext().getRealPath("upload"));
		System.out.println("uploadPath=====" + uploadPath);
		if (!uploadPath.exists()) {
			uploadPath.mkdir();
		}
		super.init();
	}
    public FileUpload()   
    {  
        super();  
    }  
   public void destroy()   
    {  
        super.destroy();   
    }        
    @SuppressWarnings("unchecked")  
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException  
    {  
        System.out.println("Access!");  
        response.setContentType("text/html;charset=UTF-8");  
        request.setCharacterEncoding("UTF-8");
       PrintWriter out = response.getWriter();  
   
        DiskFileItemFactory factory = new DiskFileItemFactory();  
        factory.setSizeThreshold(4096);  
        ServletFileUpload upload = new ServletFileUpload(factory);  
        upload.setSizeMax(maxPostSize);  
        try  
        {  
            List fileItems = upload.parseRequest(request);  
            Iterator iter = fileItems.iterator();  
            while(iter.hasNext())  
           {  
               FileItem item = (FileItem)iter.next();  
               if(!item.isFormField())  
               {  
                   String name = item.getName();  
                  String lx=request.getParameter("lx");
                  // add by cuiyi  取到文件名称
                  String filename = request.getParameter("filename");
                  File path = null;
                  //modify by cuiyi 20130330 
                  path=new File(uploadPath+"\\"+lx); 
                 
                   if (!path.exists()) {
                	   path.mkdir();
           		}
                   System.out.println(filename);  
                    try  
                   {  
                    	item.write(new File(path+"\\"+filename));  
                    }  
                   catch(Exception e)  
                   {  
                       e.printStackTrace();  
                    }  
               }  
           }  
        }  
        catch(FileUploadException e)  
        {  
           e.printStackTrace();  
        }  
    }  
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException   
    {  
        processRequest(request, response);  
    }  
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException   
    {  
        processRequest(request, response);  
    }  
}